home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 with MFC / Programming Windows 95 with MFC (Microsoft Programming Series)(097-0001465)(1996).iso / CODE / Chap09 / Paint6 / TextView.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-05  |  2.0 KB  |  79 lines

  1. //***********************************************************************
  2. //
  3. //  TextView.cpp
  4. //
  5. //***********************************************************************
  6.  
  7. #include <afxwin.h>
  8. #include <afxext.h>
  9.  
  10. #include "Resource.h"
  11. #include "CLine.h"
  12. #include "Paint6Doc.h"
  13. #include "TextView.h"
  14.  
  15. IMPLEMENT_DYNCREATE (CTextView, CEditView)
  16.  
  17. BEGIN_MESSAGE_MAP (CTextView, CEditView)
  18.     ON_WM_CREATE ()
  19. END_MESSAGE_MAP ()
  20.  
  21. BOOL CTextView::PreCreateWindow (CREATESTRUCT& cs)
  22. {
  23.     if (!CEditView::PreCreateWindow (cs))
  24.         return FALSE;
  25.  
  26.     cs.style |= ES_MULTILINE | ES_AUTOHSCROLL | ES_AUTOVSCROLL |
  27.         ES_READONLY | WS_VSCROLL;
  28.     return TRUE;
  29. }
  30.  
  31. int CTextView::OnCreate (LPCREATESTRUCT lpcs)
  32. {
  33.     if (CEditView::OnCreate (lpcs) == -1)
  34.         return -1;
  35.  
  36.     CClientDC dc (this);
  37.     int nHeight = -((dc.GetDeviceCaps (LOGPIXELSY) * 8) / 72);
  38.  
  39.     m_font.CreateFont (nHeight, 0, 0, 0, FW_NORMAL, 0, 0, 0,
  40.         DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS,
  41.         DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "MS Sans Serif");
  42.  
  43.     SetFont (&m_font, FALSE);
  44.     return 0;
  45. }
  46.  
  47. void CTextView::OnInitialUpdate ()
  48. {
  49.     GetEditCtrl ().SetWindowText ("");
  50.  
  51.     CPaintDoc* pDoc = GetDocument ();
  52.     int nCount = pDoc->GetLineCount ();
  53.  
  54.     if (nCount > 0) {
  55.         for (int i=0; i<nCount; i++)
  56.             AddEntry (pDoc->GetLine (i));
  57.     }
  58. }
  59.  
  60. void CTextView::OnUpdate (CView* pSender, LPARAM lHint, CObject* pHint)
  61. {
  62.     if (pHint != NULL) {
  63.         AddEntry ((CLine*) pHint);
  64.         return;
  65.     }
  66.     CEditView::OnUpdate (pSender, lHint, pHint);
  67. }
  68.  
  69. void CTextView::AddEntry (CLine* pLine)
  70. {
  71.     int nLine = GetEditCtrl ().GetLineCount () - 1;
  72.     int nIndex = GetEditCtrl ().LineIndex (nLine);
  73.     GetEditCtrl ().SetSel (nIndex, nIndex);
  74.  
  75.     BOOL bDocState = GetDocument ()->IsModified ();
  76.     GetEditCtrl ().ReplaceSel (pLine->GetDescription ());
  77.     GetDocument ()->SetModifiedFlag (bDocState);
  78. }
  79.